﻿using System;
using Newtonsoft.Json;

namespace InterfaceLiftLib.Converters.Json
{
    public class UnixTimestampToDateTimeConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof (int) || objectType == typeof (long);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var secondsFromEpoch = (long) reader.Value;
            var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(secondsFromEpoch);            
            return dateTime;
        }
    }
}
